package insultGen.systemFiles;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;

public class InsultGenActivity extends Activity {
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setTitle("InsultGen Beta");
        SharedPreferences pre = getPreferences(MODE_PRIVATE);
        
        //TODO place "!" infront of "pre.getBoolean("started", false)" if its not working on the first try
        if(!pre.getBoolean("started", false)){
        	firstUse(pre);
        }
        
        
        
        final Gen gen = new Gen();
        
        final EditText Editer = (EditText) findViewById(R.id.editText1);
        
        Button button = (Button) findViewById(R.id.genButton);
        button.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {				
				Editer.setText(gen.insult(getVerb(), getNoun(), getAdj()));
			}
		});
        Button optionsButton = (Button) findViewById(R.id.options);
        optionsButton.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {	
				startActivity(new Intent(getApplication(), InsultOptions.class));
			}
		});
    }
    
    public String[] readLines(String file){
        InputStreamReader isr = null;
        FileInputStream fIn = null;
        BufferedReader br = null;
        String lineBuffer = null;
        List<String> buffer = new ArrayList<String>();
        try {
        	fIn = openFileInput(file);
        	isr = new InputStreamReader(fIn);
        	br = new BufferedReader(isr);
        	while ((lineBuffer = br.readLine()) != null) {
                buffer.add(lineBuffer);
            }
        } catch (Exception e) {
        	e.printStackTrace();
        }
        String[] data = buffer.toArray(new String[buffer.size()]);
        
        return data;
    }
    
    public void writeLines(String file, String[] words){
    	FileOutputStream fOut = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            fOut = openFileOutput(file, MODE_PRIVATE);
            osw = new OutputStreamWriter(fOut);
            bw = new BufferedWriter(osw);
            for(int i = 0; i < words.length; i++){
            	bw.write(words[i]);
            	if(i < words.length){
            		bw.newLine();
            	}
            }
            bw.flush();
            bw.close();
        }

        catch (Exception e) {

            e.printStackTrace();
        }
    }
    
    public String[] getVerb(){
    	
    	List<String> verbBuffer = new ArrayList<String>();
    	String[] temp = readLines("verb.txt");
    	for(int i = 0; i < temp.length; i++){
    		verbBuffer.add(temp[i]);
    	}
    	temp = null;
    	if(Options.getRaceID() == R.id.black){
    		temp = readLines("race_" + Options.getRace() + "_verb.txt");
    		for(int i = 0; i < temp.length; i++){
        		verbBuffer.add(temp[i]);
        	}
    		temp = null;
    	}
    	 
    	String[] finalData = verbBuffer.toArray(new String[verbBuffer.size()]);
    	
    	return finalData;
    }
    
    public String[] getNoun(){
    	
    	List<String> nounBuffer = new ArrayList<String>();
    	String[] temp = readLines("noun.txt");
    	for(int i = 0; i < temp.length; i++){
    		nounBuffer.add(temp[i]);
    	}
    	temp = null;
    	if(Options.getRaceID() != R.id.none){
    		temp = readLines("race_" + Options.getRace() + "_noun.txt");
    		for(int i = 0; i < temp.length; i++){
        		nounBuffer.add(temp[i]);
        	}
    		temp = null;
    	}
    	
    	String[] finalData = nounBuffer.toArray(new String[nounBuffer.size()]);
    	
    	return finalData;
    }
    
    public String[] getAdj(){
    	
    	List<String> adjBuffer = new ArrayList<String>();
    	String[] temp = readLines("adj.txt");
    	for(int i = 0; i < temp.length; i++){
    		adjBuffer.add(temp[i]);
    	}
    	temp = null;
    	if(Options.getRaceID() != R.id.none){
    		temp = readLines("race_"+ Options.getRace() + "_adj.txt");
    		for(int i = 0; i < temp.length; i++){
        		adjBuffer.add(temp[i]);
        	}
    		temp = null;
    	}
    	
    	String[] finalData = adjBuffer.toArray(new String[adjBuffer.size()]);
    	
    	return finalData;
    }
    private void firstUse(SharedPreferences pre){
    	writeLines("verb.txt", Create.newVerbs());
    	writeLines("noun.txt", Create.newNouns());
    	writeLines("adj.txt", Create.newAdjs());
    	writeLines("race_black_verb.txt", Create.Race_Black_Verb());
    	writeLines("race_black_noun.txt", Create.Race_Black_Noun());
    	writeLines("race_black_adj.txt", Create.Race_Black_Adj());
    	writeLines("race_mexican_verb.txt", Create.Race_Mexican_Verb());
    	writeLines("race_mexican_noun.txt", Create.Race_Mexican_Noun());
    	writeLines("race_mexican_adj.txt", Create.Race_Mexican_Adj());
    	writeLines("race_asain_verb.txt", Create.Race_Asian_Verb());
    	writeLines("race_asain_noun.txt", Create.Race_Asian_Noun());
    	writeLines("race_asain_adj.txt", Create.Race_Asian_Adj());
    	SharedPreferences.Editor editpre = pre.edit();
    	editpre.putBoolean("started", true);
    	editpre.commit();
    }
}